
// Definition for a binary tree node.
function TreeNode(val, left, right) {
    this.val = (val===undefined ? 0 : val)
    this.left = (left===undefined ? null : left)
    this.right = (right===undefined ? null : right)
}

/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var getLonelyNodes = function(root) {
  if(!root || (!root.left && !root.right)) return [];
  let result = [];
  function preView(node, isSignal) {
    if(!node) return;
    if(isSignal) {
      result.push(node.val);
    }
    if(!node.left && !node.right) {
      return;
    } else if(!node.left) {
      preView(node.right, true);
    } else if(!node.right) {
      preView(node.left, true);
    } else {
      preView(node.left, false);
      preView(node.right, false);
    }
  }
  preView(root, false);
  return result;
};
